package com.gzzyy.facerecognition

import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.gzzyy.facerecognition.bean.HumanDataBean
import com.gzzyy.facerecognition.bean.UrlBean
import com.gzzyy.facerecognition.http.DataResponse
import com.gzzyy.facerecognition.http.SuspendRequest
import kotlinx.coroutines.launch
import java.io.File

class MainViewModel : ViewModel() {
    private val uploadImageResponse = MutableLiveData<DataResponse<UrlBean?>>()
    fun getUploadImageResponse(): LiveData<DataResponse<UrlBean?>> = uploadImageResponse

    fun uploadImage(absolutePath: String) = viewModelScope.launch {
        val currentTime = System.currentTimeMillis()
        Log.e("test", "before upload currentTime = $currentTime")
        val response = SuspendRequest.upLoadImage(File(absolutePath))
        uploadImageResponse.postValue(response)
        val afterTime = System.currentTimeMillis()
        Log.e("test", "after upload time = $afterTime")
        Log.e("test", "upload timeGap  = ${afterTime - currentTime})")
    }

    private val uploadUrlResponse = MutableLiveData<DataResponse<HumanDataBean?>>()
    fun getUploadUrlResponse(): LiveData<DataResponse<HumanDataBean?>> = uploadUrlResponse
    fun uploadUrl(imageUrl: String) = viewModelScope.launch {
        val currentTime = System.currentTimeMillis()
        Log.e("test", "before uploadUrl currentTime = $currentTime")
        val response = SuspendRequest.upLoadUrl(imageUrl)
        uploadUrlResponse.postValue(response)
        val afterTime = System.currentTimeMillis()
        Log.e("test", "after uploadUrl time = $afterTime")
        Log.e("test", "uploadUrl timeGap  = ${afterTime - currentTime})")
    }


}